﻿/*
 * 描述：提供HTTP的POST/GET操作
 * 作者：Zony
 * 创建日期：2016/10/05
 * 最后修改日期：2016/10/09
 * 版本：v1.1
 */
using System;
using System.IO;
using System.Net;
using System.Text;

namespace LibNet.Http
{
    /// <summary>
    /// 提供HTTP的POST/GET操作
    /// </summary>
    public class NetUtils
    {
        /// <summary>
        /// 提供对HTTP访问的GET操作
        /// </summary>
        /// <param name="url">目标URL</param>
        /// <param name="referer">标头</param>
        /// <param name="encoding">编码方式</param>
        /// <returns></returns>
        public string HttpGet(string url,string referer,Encoding encoding)
        {
            try
            {
                HttpWebRequest _req = (HttpWebRequest)WebRequest.Create(url);
                _req.Method = "get";
                _req.ContentType = "application/x-www-form-urlencoded";

                if (referer != null)
                {
                    _req.Referer = referer;
                }

                HttpWebResponse _res = (HttpWebResponse)_req.GetResponse();
                if (_res.StatusCode == HttpStatusCode.Forbidden) return null;
                StringBuilder _sb = new StringBuilder();

                using (Stream _st = _res.GetResponseStream())
                {
                    using (StreamReader _sr = new StreamReader(_st, encoding))
                    {
                        string _line;
                        while ((_line = _sr.ReadLine()) != null)
                        {
                            _sb.Append(_line);
                        }
                        return _sb.ToString();
                    }
                }
            }catch
            {
                return null;
            }
        }

        /// <summary>
        /// 提供对HTTP访问的POST操作
        /// </summary>
        /// <param name="url">目标URL</param>
        /// <param name="referer">标头</param>
        /// <param name="encoding">编码方式</param>
        /// <param name="data">提交的数据</param>
        /// <returns></returns>
        public string HttpPost(string url,string referer,Encoding encoding,string data)
        {
            try
            {
                HttpWebRequest _req = (HttpWebRequest)WebRequest.Create(url);
                byte[] _postData = encoding.GetBytes(data);

                _req.Method = "post";
                _req.Referer = referer;
                _req.ContentType = "application/x-www-form-urlencoded";
                _req.ContentLength = _postData.Length;

                using (Stream _st = _req.GetRequestStream())
                {
                    _st.Write(_postData, 0, _postData.Length);
                }

                HttpWebResponse _res = (HttpWebResponse)_req.GetResponse();
                using (StreamReader _sr = new StreamReader(_res.GetResponseStream(), encoding))
                {
                    string _result = _sr.ReadToEnd();
                    return _result;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 将字符串进行URL编码
        /// </summary>
        /// <param name="str">需要编码的字符串</param>
        /// <param name="encoding">编码方式，代码页</param>
        /// <returns></returns>
        public string URL_Encoding(string str,Encoding encoding)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = encoding.GetBytes(str);

            for(int i=0;i<byStr.Length;i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }

            return sb.ToString();
        }
    }
}
